﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Data.Models;

namespace Parafarmacia
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var ProductoID = Convert.ToInt16(Page.RouteData.Values["Id"]);

                using (var db = new dbParafarmaciaContext())
                {
                    var productos = from p in db.tblProductos where p.Id == ProductoID select p;

                    foreach (tblProducto prod in productos)
                    {
                        lblProductName.Text = prod.NombreProducto;
                        lblDescription.Text = prod.Detalle;
                        lblPrecio.Text = prod.Precio + " €";

                        var marca = from m in db.tblMarcas where m.Id == prod.IdMarca select m;
                        try
                        {
                            lblMarca.Text = marca.First().NombreMarca;
                        }
                        catch
                        {
                            lblMarca.Text = "";
                        }
                        ProductImage.ImageUrl = prod.imageUrl;
                        ProductImage.AlternateText = prod.NombreProducto;
                    }
                }
            }
        }

        protected void ListViewImages_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "ImagenCambiar")
            {
                ProductImage.ImageUrl = e.CommandArgument.ToString();
            }
        }


        protected void butAñadirCarrito_Click(object sender, EventArgs e)
        {
            using (var db = new dbParafarmaciaContext())
            {
                tblPedido pedido = new tblPedido();

                pedido.FacturarNombre = "roman";
                pedido.FechaCompra = System.DateTime.Now.Date;

                db.tblPedidos.Add(pedido);
                db.SaveChanges();
            }
        }
    }
}